/*
 * Copyright © OpenAtom Foundation.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *     http://www.apache.org/licenses/LICENSE-2.0
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package io.iec.edp.caf.databaseobject.rttest;

import io.iec.edp.caf.boot.context.CAFContext;
import io.iec.edp.caf.commons.utils.SpringBeanUtils;
import io.iec.edp.caf.database.Database;
import io.iec.edp.caf.database.DatabaseManager;
import io.iec.edp.caf.databaseobject.api.entity.AbstractDatabaseObject;
import io.iec.edp.caf.databaseobject.api.entity.ScopeType;
import io.iec.edp.caf.databaseobject.api.entity.TempTableContext;
import io.iec.edp.caf.databaseobject.api.service.IDatabaseObjectDeployService;
import io.iec.edp.caf.databaseobject.api.service.IDatabaseObjectDtService;
import io.iec.edp.caf.databaseobject.api.service.IDatabaseObjectRtService;
import io.iec.edp.caf.databaseobject.api.service.IDatabaseObjectTempTableService;
import io.iec.edp.caf.databaseobject.rttest.api.IDatabaseObjectRtTestService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.EntityManager;
import javax.swing.*;
import javax.ws.rs.core.Response;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author : liu_wei
 * @date : 2022-02-15 11:37
 **/
@Slf4j
public class DatabaseObjectRtTestServiceImpl implements IDatabaseObjectRtTestService {

    private IDatabaseObjectRtService databaseObjectRtService = SpringBeanUtils.getBean(IDatabaseObjectRtService.class);
    private IDatabaseObjectDeployService databaseObjectDeployService = SpringBeanUtils.getBean(IDatabaseObjectDeployService.class);
    private IDatabaseObjectDtService databaseObjectDtService = SpringBeanUtils.getBean(IDatabaseObjectDtService.class);
    private IDatabaseObjectTempTableService databaseObjectTempTableService= SpringBeanUtils.getBean(IDatabaseObjectTempTableService.class);
    @Override
    public Response getDtDatabaseById(String id) {
        try {
            Object result = databaseObjectDtService.getDtDatabaseObject(id);
            return Response.ok().entity(result).build();
        }catch(Exception ex){
            log.error(ex.getMessage(), ex);
            return Response.serverError().entity(ex.getMessage()).build();
        }
    }

    @Override
    public Response getSuDtDatabaseById(String id, String su) {
        try {
            Object result = databaseObjectDtService.getDtDatabaseObject(id, su);
            return Response.ok().entity(result).build();
        }catch(Exception ex){
            log.error(ex.getMessage(), ex);
            return Response.serverError().entity(ex.getMessage()).build();
        }
    }

    @Override
    public Response getDatabaseObject(String id, String su) {
        try {
            Object result = databaseObjectRtService.getDatabaseObject(id, su);
            return Response.ok().entity(result).build();
        }catch(Exception ex){
            log.error(ex.getMessage(), ex);
            return Response.serverError().entity(ex.getMessage()).build();
        }
    }

    @Override
    public Response getDatabaseObjectByCode(String code, String su) {
        try {
            Object result = databaseObjectRtService.getDatabaseObjectByCode(code, su);
            return Response.ok().entity(result).build();
        }catch(Exception ex){
            log.error(ex.getMessage(), ex);
            return Response.serverError().entity(ex.getMessage()).build();
        }
    }

//    @Override
//    public Response getDatabaseObjectList(String su) {
//        try {
//            Object result = databaseObjectRtService.getDatabaseObjectList(su);
//            return Response.ok().entity(result).build();
//        }catch(Exception ex){
//            log.error(ex.getMessage(), ex);
//            return Response.serverError().entity(ex.getMessage()).build();
//        }
//    }

    @Override
    public Response getDtDatabaseObjectByCode(String code, String su) {
        try {
            Object result = databaseObjectDtService.getDtDatabaseObjectByCode(code, su);
            return Response.ok().entity(result).build();
        }catch(Exception ex){
            log.error(ex.getMessage(), ex);
            return Response.serverError().entity(ex.getMessage()).build();
        }
    }

    @Override
    public Response saveDtDatabaseObject(String id, String su) {
        try {
            databaseObjectDtService.saveDtDatabaseObject(databaseObjectRtService.getDatabaseObject(id), su);
            return Response.ok().build();
        }catch(Exception ex){
            log.error(ex.getMessage(), ex);
            return Response.serverError().entity(ex.getMessage()).build();
        }
    }

    @Override
    public Response deleteDtDatabaseObject(String id, String su) {
        try {
            databaseObjectDtService.deleteDtDatabaseObject(id, su);
            return Response.ok().build();
        }catch(Exception ex){
            log.error(ex.getMessage(), ex);
            return Response.serverError().entity(ex.getMessage()).build();
        }
    }

    @Override
    public Response isExistDatabaseObjectDt(String id, String su) {
        try {
            Object result = databaseObjectDtService.isExistDatabaseObjectDt(id, su);
            return Response.ok().entity(result).build();
        }catch(Exception ex){
            log.error(ex.getMessage(), ex);
            return Response.serverError().entity(ex.getMessage()).build();
        }
    }

    @Override
    public Response deployDatabaseObject(String id, String su) {
        try {
            Map<String, String> dimensionInfo = new HashMap<>();
            dimensionInfo.put("FIYear", "2022");
            databaseObjectDeployService.deployDatabaseObject(databaseObjectRtService.getDatabaseObject(id, su), dimensionInfo, su);
            return Response.ok().build();
        }catch(Exception ex){
            log.error(ex.getMessage(), ex);
            return Response.serverError().entity(ex.getMessage()).build();
        }
    }

    @Override
    public Response deployDatabaseObjects(String id, String su) {
        try {
            Map<String, String> dimensionInfo = new HashMap<>();
            dimensionInfo.put("FIYear", "2022");
            AbstractDatabaseObject object = databaseObjectRtService.getDatabaseObject(id, su);
            List<AbstractDatabaseObject> databaseObjects = new ArrayList<>();
            databaseObjects.add(object);
            Object result = databaseObjectDeployService.deployDatabaseObjects(databaseObjects,dimensionInfo , su);
            return Response.ok().entity(result).build();
        }catch(Exception ex){
            log.error(ex.getMessage(), ex);
            return Response.serverError().entity(ex.getMessage()).build();
        }
    }

    @Override
    public Response deployTempTableDatabaseObjects(String dboid) {
        try{
            DatabaseManager databaseManager = DatabaseManager.getInstance();
            databaseManager.begin();
            Database database = databaseManager.getDatabase();
            TempTableContext tempTableContext=null;
            tempTableContext = databaseObjectTempTableService.createFixedTable(database, ScopeType.Session, dboid);
            String sql1="insert into "+tempTableContext.getTableName()+" values('test0','test0')";
            String sql2="select * from "+tempTableContext.getTableName();
            System.out.println(database.execute(sql1));
            ResultSet resultSet=database.query(sql2);
            ResultSetMetaData rsmd = resultSet.getMetaData();
            int numberOfColumns = rsmd.getColumnCount();
            resultSet.next();
            for(int i=0;i<numberOfColumns;i++){
                System.out.println(resultSet.getString(i+1));
            }
            databaseManager.end();
        }catch (Exception e){
            log.error("临时表异常：" + e);
        }
        return null;
    }

    @Override
    @Transactional
    public Response deployTransactionTempTableDatabaseObjects(String dboid) {
        EntityManager em = SpringBeanUtils.getBean(EntityManager.class);
        TempTableContext tempTableContext = databaseObjectTempTableService.createFixedTable(null, ScopeType.Transactional, dboid);
        String sessionId = CAFContext.current.getSessionId();
        String tempTableCode = dboid + sessionId.substring(sessionId.length() - 10);

        String sql1="insert into "+tempTableCode+" values('test0','test0')";
        String sql2="select * from "+tempTableCode;
        em.createNativeQuery(sql1).executeUpdate();
        List result = em.createNativeQuery(sql2).getResultList();
        return null;
    }
}
